﻿/*
 * Author: Brian Brewder
 * Website: http://redwerb.com
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Redwerb.Visualizers.Controls;

namespace Redwerb.Visualizers.ExceptionVisualizer
{
    public partial class ExceptionVisualizerDialog : Form
    {
        
        #region Initialization and Destruction

        public ExceptionVisualizerDialog()
        {
            InitializeComponent();
        }

        #endregion

        #region Fields and Properties

        private Exception mException;
        public Exception Exception
        {
            get { return mException; }
            set 
            {
                cboException.Items.Clear();
                mException = value; 
                var exceptions = new List<Exception>();
                var ex = mException;
                while (ex != null)
                {
                    exceptions.Add(ex);
                    ex = ex.InnerException;
                }
                lblCount.Text = exceptions.Count.ToString();
                cboException.DataSource = exceptions;
                txtReport.Text = GenerateReport();
            }
        }

        #endregion

        #region Methods

        private string GenerateReport()
        {
            if (mException == null) return "";

            var sb = new StringBuilder();
            var ex = mException;
            while(ex != null)
            {
                sb.AppendLine(string.Format("[{0}] {1}", ex.GetType().FullName, ex.Message));
                sb.AppendLine(ex.StackTrace);

                ex = ex.InnerException;
                if (ex != null)
                {
                    sb.AppendLine();
                    sb.AppendLine(new string('*', 100));
                    sb.AppendLine();
                }
            }
            return sb.ToString();
        }

        #endregion

        #region Event Handlers

        private void cboException_SelectedValueChanged(object sender, EventArgs e)
        {
            var ex = cboException.SelectedValue as Exception;
            txtException.Text = string.Format("[{0}] {1}", ex.GetType().FullName, ex.Message);
            pgException.SelectedObject = ex;
            rtbStackTrace.Text = ex.StackTrace;
            //todo: highlight lines where source code is available.
            //rtbStackTrace.SelectLine(5);
            //rtbStackTrace.SelectionBackColor = Color.Yellow;
        }

        private void ExceptionVisualizerDialog_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.PageUp:
                    if (cboException.SelectedIndex > 0)
                        cboException.SelectedIndex -= 1;
                    e.Handled = true;
                    break;
                case Keys.PageDown:
                    if (cboException.SelectedIndex < cboException.Items.Count - 1)
                        cboException.SelectedIndex += 1;
                    e.Handled = true;
                    break;
            }
        }

        #endregion

        //protected override bool ProcessDialogKey(Keys keyData)
        //{
        //    switch (keyData)
        //    {
        //        case Keys.Tab:
        //            if (tabException.SelectedTab == pagProperties)
        //                tabException.SelectedTab = pagStackTrace;
        //            else
        //                tabException.SelectedTab = pagProperties;
        //            return false;
        //        case Keys.Up:
        //            if (cboException.SelectedIndex > 0)
        //                cboException.SelectedIndex -= 1;
        //            return false;
        //        case Keys.Down:
        //            if (cboException.SelectedIndex < cboException.Items.Count - 1)
        //                cboException.SelectedIndex += 1;
        //            return false;
        //        case Keys.Left:
        //            if (tabException.SelectedIndex > 0)
        //                tabException.SelectedIndex -= 1;
        //            return false;
        //        case Keys.Right:
        //            if (tabException.SelectedIndex < tabException.TabCount - 1)
        //                tabException.SelectedIndex += 1;
        //            return false;
        //    }
        //    return base.ProcessDialogKey(keyData);
        //}

    }
}
